//
//  MedicalRecordViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/19.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit
/// 我的病历
class MedicalRecordViewController: BaseViewController {
    
    private let viewModel = MedicalRecordViewModel()
    
    private var diagnoseId: String = ""
    
    convenience init(with diagnoseId: String) {
        self.init()
        self.diagnoseId = diagnoseId
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的病历"
        
        loadData()
    }
    
    override func makeUI() {
        super.makeUI()
        
        tableView.tableHeaderView = tableHeaderView
        tableHeaderView.snp.makeConstraints { make in
            make.width.equalTo(tableView)
        }
        tableView.tableHeaderView?.layoutIfNeeded()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide).priority(.low)
            make.bottom.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        viewModel.updateClosure = { [weak self] in
            self?.updateUI()
        }
    }
    
    private func updateUI() {
        tableView.isHidden = false
        tableHeaderView.bind(to: viewModel)
        tableView.reloadData()
    }
    
    private func loadData() {
        viewModel.getMedicalRecordDetail(diagnoseId: diagnoseId)
    }
    
    private lazy var tableHeaderView = UINib.loadViewFrom(MedicalRecordTableHeaderView.self)

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .background
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.isHidden = true
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(MedicalRecordPatientInfoCell.nibForCell(),
                           forCellReuseIdentifier: MedicalRecordPatientInfoCell.reuseIdentifier())
        tableView.register(MedicalRecordDetailCell.nibForCell(),
                           forCellReuseIdentifier: MedicalRecordDetailCell.reuseIdentifier())
        tableView.register(MedicalRecordPictureCell.nibForCell(),
                           forCellReuseIdentifier: MedicalRecordPictureCell.reuseIdentifier())
        tableView.register(MedicalRecordSignCell.nibForCell(),
                           forCellReuseIdentifier: MedicalRecordSignCell.reuseIdentifier())
        
        return tableView
    }()

}

extension MedicalRecordViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let viewModel = viewModel.viewModel(in: indexPath.section)
        
        switch viewModel.mode {
        case .patientInfo:
            let cell = tableView.dequeueReusableCell(withIdentifier: MedicalRecordPatientInfoCell.reuseIdentifier(),
                                                     for: indexPath) as! MedicalRecordPatientInfoCell
            cell.bind(to: viewModel)
            return cell
        case .picture:
            let cell = tableView.dequeueReusableCell(withIdentifier: MedicalRecordPictureCell.reuseIdentifier(),
                                                     for: indexPath) as! MedicalRecordPictureCell
            cell.bind(to: viewModel)
            cell.imageClickedClosure = { [weak self] (imageUrls, index) in
                guard let self = self else { return }
                let brows = JSImagBrows(parentController: self,
                                        images: imageUrls,
                                        andCurrentIndex: index,
                                        andPlaceHolderImage: nil)
                brows?.modalPresentationStyle = .fullScreen
                brows?.show()
            }
            return cell
        case .sign:
            let cell = tableView.dequeueReusableCell(withIdentifier: MedicalRecordSignCell.reuseIdentifier(),
                                                     for: indexPath) as! MedicalRecordSignCell
            cell.bind(to: viewModel)
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: MedicalRecordDetailCell.reuseIdentifier(),
                                                     for: indexPath) as! MedicalRecordDetailCell
            cell.bind(to: viewModel)
            return cell
        }
    }
}

extension MedicalRecordViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return viewModel.headerHeight(in: section)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
}
